#ifndef __TOYM__NET__TCP_CLIENT_H__
#define __TOYM__NET__TCP_CLIENT_H__

#include <string>
#include <atomic>
#include "net/Callables.h"
#include "net/Connector.h"
#include "net/InetAddress.h"
#include "net/TcpConnection.h"
#include "Base/Buffer.h"
#include "Base/Timestamp.h"

namespace toym
{
namespace net
{

class EventLoop;
class Socket;

class TcpClient {
public:
    TcpClient(EventLoop* loop, const InetAddress& serverAddr, const std::string& clientName = "TcpClient");
    
    ~TcpClient();

    void setConnectionCallback(const ConnectionCallback_t& cb) {
        this->m_conntionCb = cb;
    }
    void setConnectionCallback(ConnectionCallback_t&& cb) {
        this->m_conntionCb = std::move(cb);
    }

    void setMessageCallback(const MessageCallback_t& cb) {
        this->m_messageCb = cb;
    }
    void setMessageCallback(MessageCallback_t&& cb) {
        this->m_messageCb = std::move(cb);
    }

    void setWriteComplateCallback(const WriteComplateCallback_t& cb) {
        this->m_writeComplateCb = cb;
    }
    void setWriteComplateCallback(WriteComplateCallback_t&& cb) {
        this->m_writeComplateCb = std::move(cb);
    }

    void start() {
        m_connector.start();
    }
    void stop() {
        m_connector.stop();
    }

    void disconnect();

private:

    void disconnectInLoop();
    void connectedCallback(int sockfd);
    void removeConnection(TcpConnectionPtr conn);
    void removeConnectionInLoop(TcpConnectionPtr conn);

    static
    void defaultConnectionCallback(const TcpConnectionPtr&);
    static
    void defaultMessageCallback(const TcpConnectionPtr&, Buffer*, Timestamp);
    // static
    // void defaultWriteComplateCallback(const TcpConnectionPtr&);
    //   没有这个函数

    EventLoop* m_loop;
    std::string m_clientName;
    Connector m_connector;
    TcpConnectionPtr m_conn;
    ConnectionCallback_t m_conntionCb;
    MessageCallback_t m_messageCb;
    WriteComplateCallback_t m_writeComplateCb;
    InetAddress m_serverAddr;

    static std::atomic_uint64_t s_connSeq;
};

} // namespace net

} // namespace toym


#endif